package com.whty.cms.filewps.config.service.impl;

import cn.hutool.json.JSONUtil;
import com.whty.cms.filewps.common.constants.Consts;
import com.whty.cms.filewps.common.service.impl.BaseAbstractServiceImpl;
import com.whty.cms.filewps.config.dao.ApplyInfoMapper;
import com.whty.cms.filewps.config.dao.ConfigureMapper;
import com.whty.cms.filewps.config.dto.ApplyInfo;
import com.whty.cms.filewps.config.dto.Configure;
import com.whty.cms.filewps.config.service.IApplyInfoService;
import com.whty.cms.filewps.util.MemcacheUtil;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.UUID;

@Service("applyInfoService")
public class ApplyInfoServiceImpl extends BaseAbstractServiceImpl<ApplyInfo> implements IApplyInfoService{
	
	@Resource
	private ApplyInfoMapper applyInfoMapper;
	
	@Resource
	private ConfigureMapper configureMapper;

	@Override
	public void saveApplyInfo(JSONObject json) throws Exception {
		ApplyInfo applyInfo = new ApplyInfo();
		String appId = UUID.randomUUID().toString().replaceAll("-", "");
		applyInfo.setAppId(appId);
		applyInfo.setAppName(json.optString("appName"));
		applyInfo.setSourceBucket(json.optString("sourceBucket")+"-"+Consts.COS_APPID);
		applyInfo.setDestBucket(json.optString("destBucket")+"-"+Consts.COS_APPID);
		if(json.containsKey("transformFlag") && StringUtils.isNotBlank(json.getString("transformFlag"))) {
			applyInfo.setTransformFlag(json.getString("transformFlag"));
		}
		applyInfo.setCreateTime(new Date());
		applyInfo.setUpdateTime(new Date());
		applyInfoMapper.insert(applyInfo);
		
		Configure conSource = new Configure();
		conSource.setKey(appId+"-"+Consts.SOURCE_BUCKET);
		conSource.setValue(json.optString("sourceBucket")+"-"+Consts.COS_APPID);
		configureMapper.insert(conSource);
		
		Configure conSourcePub = new Configure();
		conSourcePub.setKey(json.optString("sourceBucket")+"-"+Consts.COS_APPID);
		conSourcePub.setValue(json.optString("public"));
		configureMapper.insert(conSourcePub);
		
		Configure conDestPub = new Configure();
		conDestPub.setKey(json.optString("destBucket")+"-"+Consts.COS_APPID);
		conDestPub.setValue(json.optString("public"));
		configureMapper.insert(conDestPub);
		
		Configure conDest = new Configure();
		conDest.setKey(appId+"-"+Consts.DEST_BUCKET);
		conDest.setValue(json.optString("destBucket")+"-"+Consts.COS_APPID);
		configureMapper.insert(conDest);
		
		Configure conUpload = new Configure();
		conUpload.setKey(appId);
		conUpload.setValue("0");
		configureMapper.insert(conUpload);

		Configure conCustomSource = new Configure();
		conCustomSource.setKey(appId+"-customSourceBucket");
		conCustomSource.setValue("https://"+applyInfo.getSourceBucket()+Consts.CUSTOM_HOST);
		configureMapper.insert(conCustomSource);

		Configure conCustomDest = new Configure();
		conCustomDest.setKey(appId+"-customDestBucket");
		conCustomDest.setValue("https://"+applyInfo.getDestBucket()+Consts.CUSTOM_HOST);
		configureMapper.insert(conCustomDest);

		Configure conPreviewVersion = new Configure();
		conPreviewVersion.setKey(appId+"-previewVersion");
		conPreviewVersion.setValue("win");
		configureMapper.insert(conPreviewVersion);
	}

	@Override
	public ApplyInfo findOneById(String id) {
		String data = (String) MemcacheUtil.getObj(Consts.getMemApplyinfoKey(id));
		if(StringUtils.isBlank(data)) {
			ApplyInfo applyInfo = applyInfoMapper.selectByPrimaryKey(id);
			if(applyInfo != null) {
				MemcacheUtil.putObj(Consts.getMemApplyinfoKey(id), JSONUtil.parseObj(applyInfo).toString(), 24*60*60);
			}
			return applyInfo;
		}
		return JSONUtil.toBean(data,ApplyInfo.class);
	}

}
